#include "pch.h"
#include "Application.h"
#include "Live/Events/ApplicationEvent.h"
#include "Live/Log.h"
#include "Live/Layer/Layer.h"

#include <GLFW/glfw3.h>
namespace Live
{
#define BIND_EVENT_FN(x) std::bind(&Application::x,this,std::placeholders::_1)

    Application::Application()
    {
        m_Window = std::unique_ptr<Window>(Window::Create());

        m_Window->SetEventCallback(BIND_EVENT_FN(OnEvent));
    }

    Application::~Application()
    {
    }

    void Application::OnEvent(Event& e)
    {
        EventDispatcher dispatcher(e);
        dispatcher.Dispatch<WindowCloseEvent>(BIND_EVENT_FN(OnWindowClose));

        //LE_COER_INFO(e);

        for (auto it = m_LayerStack.end(); it != m_LayerStack.begin();) 
        {
            (*--it)->OnEvent(e);
            if (e.Handled)
                break;
        }
    }

    void Application::Run()
    {       
        while (m_Runing)
        {
            glClearColor(1,0,1,1);
            glClear(GL_COLOR_BUFFER_BIT);

            for (Layer* layer : m_LayerStack)
                layer->OnUpdate();

            m_Window->OnUpdate();
        }
    }

    void Application::PushLayer(Layer* layer)
    {
        m_LayerStack.PushLayer(layer);
    }

    void Application::PushOverlay(Layer* layer)
    {
        m_LayerStack.PushOvrlay(layer);

    }

    bool Application::OnWindowClose(WindowCloseEvent& e)
    {
        m_Runing = false;
        return true;
    }

  

}